package com.zb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjGoodsFieldContent;
import com.zb.mapper.ZjGoodsFieldContentMapper;
import com.zb.service.ZjGoodsFieldContentService;
import com.zb.vo.GoodsFieldContentVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 字段内容表，当category=3/4时，内容存入该表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-08-01
 */
@Service
public class ZjGoodsFieldContentServiceImpl extends ServiceImpl<ZjGoodsFieldContentMapper, ZjGoodsFieldContent> implements ZjGoodsFieldContentService {
    @Resource
    private ZjGoodsFieldContentMapper goodsFieldContentMapper;

    // 保存或修改字段内容
    @Override
    @Transactional
    public void save(Long custAccId, Long goodsFieldId, List<GoodsFieldContentVo> contentVos) {
        if (null == contentVos || contentVos.isEmpty()) {
            return;
        }
        Assert.isTrue(contentVos.stream().map(GoodsFieldContentVo::getVal).toList().size() == contentVos.stream().map(GoodsFieldContentVo::getVal).collect(Collectors.toSet()).size(),
                "字段内容已重复！", null);

        List<ZjGoodsFieldContent> adds = new ArrayList<>();
        List<ZjGoodsFieldContent> updates = new ArrayList<>();
        ZjGoodsFieldContent content = null;
        for (GoodsFieldContentVo vo : contentVos) {
            content = new ZjGoodsFieldContent();
            if (null != vo.getId()) {
                content.setId(vo.getId());
                content.setVal(vo.getVal());
                updates.add(content);
                continue;
            }
            content.setId(IdWorker.getId());
            content.setCustAccId(custAccId);
            content.setGoodsFieldId(goodsFieldId);
            content.setVal(vo.getVal());
            adds.add(content);
        }
        if (!adds.isEmpty()) this.saveBatch(adds);
        if (!updates.isEmpty()) this.updateBatchById(updates);
    }

    // 根据字段ID集合查询单选
    @Override
    public List<ZjGoodsFieldContent> getByFieldIds(List<Long> fieldIdList) {
        if (null  == fieldIdList || fieldIdList.isEmpty()) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjGoodsFieldContent> wrapper = new QueryWrapper<>();
        wrapper.select("id", "goods_field_id", "val");
        wrapper.in("goods_field_id", fieldIdList);
        return this.list(wrapper);
    }

    // 根据商品ID字段集合删除单选/多选内容
    @Override
    public void deleteByGoodsFieldIds(List<Long> goodsFieldIdList) {
        if (null == goodsFieldIdList || goodsFieldIdList.isEmpty()) {
            return;
        }
        goodsFieldContentMapper.deleteByGoodsFieldIds(goodsFieldIdList);
    }

    // 根据商品字段ID查询
    @Override
    public List<ZjGoodsFieldContent> getByFieldId(Long goodsFieldId) {
        QueryWrapper<ZjGoodsFieldContent> wrapper = new QueryWrapper<>();
        wrapper.select("id", "goods_field_id", "val");
        wrapper.eq("goods_field_id", goodsFieldId);
        return this.list(wrapper);
    }
}
